package com.jiutong.meipengdai.ui.activitys;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.AddressBean;
import com.jiutong.meipengdai.http.model.interfaces.MyAccountBiz;
import com.jiutong.meipengdai.http.present.MyAccountPresent;
import com.jiutong.meipengdai.ui.adapter.AddAddressAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressActivity extends BaseActivity implements MyAccountBiz, AddAddressAdapter.OnChangeListener {


    public FragmentManager manager = getSupportFragmentManager();
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_add_address)
    TextView tvAddAddress;
    @Bind(R.id.rv_address)
    RecyclerView rvAddress;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_options)
    TextView tvOptions;
    @Bind(R.id.rl_comment_title)
    RelativeLayout rlCommentTitle;
    @Bind(R.id.view_line)
    View viewLine;
    @Bind(R.id.activity_address)
    RelativeLayout activityAddress;
    @Bind(R.id.title_edit_address)
    TextView tvEditAdress;
    @Bind(R.id.tv_tishi)
    TextView tvTishi;
    @Bind(R.id.ll_no_date)
    LinearLayout llNoDate;
    private int RESULT_CODE = 100;
    private MyAccountPresent persent;
    private List<AddressBean.DataBean.AddresssBean> addresss;
    private AddressBean respond;
    private AddAddressAdapter adapter;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_address;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        persent = new MyAccountPresent(this, this);
        persent.getAddressList();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tvTitle.setText("收货地址");
    }

    @OnClick({R.id.tv_add_address, R.id.iv_back, R.id.title_edit_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_address:
                if (addresss != null && addresss.size() >= 3) {
                    toastShortShow("最多保存三个地址!");
                } else {
                    openActivity(NewAddressActivity.class);
                }
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.title_edit_address:
                if (tvEditAdress.getText() == "编辑" || tvEditAdress.getText().equals("编辑")) {
                    tvEditAdress.setText("完成");
                    for (int i = 0; i < addresss.size(); i++) {
                        addresss.get(i).setEdit(true);
                    }
                    rvAddress.getAdapter().notifyDataSetChanged();
                } else if (tvEditAdress.getText() == "完成" || tvEditAdress.getText().equals("完成")) {
                    tvEditAdress.setText("编辑");
                    for (int i = 0; i < addresss.size(); i++) {
                        addresss.get(i).setEdit(false);
                    }
                    rvAddress.getAdapter().notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        tvEditAdress.setText("编辑");
        persent.getAddressList();
//        rvAddress.getAdapter().notifyDataSetChanged();
    }


    @Override
    public String getBankNum() {
        return null;
    }

    @Override
    public String getBankName() {
        return null;
    }

    @Override
    public String getCartStatus() {
        return null;
    }

    @Override
    public String getBankId() {
        return null;
    }

    @Override
    public String getMsgId() {
        return null;
    }

    @Override
    public String getLoanId() {
        return null;
    }

    @Override
    public String Consignee() {
        return null;
    }

    @Override
    public String getTelephone() {
        return null;
    }

    @Override
    public String getBigLocation() {
        return null;
    }

    @Override
    public String getDetail() {
        return null;
    }

    @Override
    public String getIsDefault() {
        return null;
    }

    @Override
    public String getOprType() {
        return null;
    }

    @Override
    public String getMainId() {
        return null;
    }

    @Override
    public void onSuccess(Object object) {
        respond = (AddressBean) object;
        if ("200".equals(respond.getResCode())) {
            addresss = respond.getData().getAddresss();

            if(addresss != null && addresss.size() > 0){
                llNoDate.setVisibility(View.GONE);
                tvEditAdress.setVisibility(View.VISIBLE);
                rvAddress.setLayoutManager(new LinearLayoutManager(this));
                adapter = new AddAddressAdapter(this, addresss, manager);
                adapter.setOnChangeListener(this);
                rvAddress.setAdapter(adapter);
                checkAddressNum();
            }
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    public void checkAddressNum() {
        if (addresss.size() == 0) {
            tvEditAdress.setVisibility(View.GONE);
        } else {
            tvEditAdress.setVisibility(View.VISIBLE);
        }

        if (addresss.size() >= 3) {
            tvAddAddress.setTextColor(Color.GRAY);
            tvTishi.setVisibility(View.VISIBLE);
        } else {
            tvAddAddress.setTextColor(getResources().getColor(R.color.orange));
            tvTishi.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void onChange(String s) {
        tvAddAddress.setTextColor(getResources().getColor(R.color.orange));
        tvTishi.setVisibility(View.INVISIBLE);
    }

}

